import { createAction } from 'redux-actions'
import { UPDATE_JOB_DETAIL_TYPE, UPDATE_JOB_COLLECT_TYPE, UPDATE_JOB_UNCOLLECT_TYPE } from './type'
import {
  JobdetailRequest,
  JobCollectRequest,
  JobUnCollectRequest
} from 'src/utils/RequestAPI'

/* async */
export const DO_REQUEST_JOB_DETAIL_ACTION = createAction(UPDATE_JOB_DETAIL_TYPE, (...args) => {
  return new Promise((resolve, reject) => {
    JobdetailRequest(...args).then(res => {
      if (res.status) {
        const detail = res.entry
        const count = {
          viewCount: detail.viewCount,
          collectCount: detail.collectionCount,
          entryCount: detail.entryCount,
          resumeCount: detail.resumeCount
        }
        resolve({
          detail,
          count
        })
      }
    })
  })
})

/* 收藏 */
export const DO_REQUEST_JOB_COLLECT_ACTION = createAction(UPDATE_JOB_COLLECT_TYPE, (...args) => {
  return new Promise((resolve, reject) => {
    JobCollectRequest(...args).then(res => {
      if (res.status) {
        resolve({
          collectionId: res.entry.id
        })
      }
    })
  })
})
export const DO_REQUEST_JOB_UNCOLLECT_ACTION = createAction(UPDATE_JOB_UNCOLLECT_TYPE, (...args) => {
  return new Promise((resolve, reject) => {
    JobUnCollectRequest(...args).then(res => {
      if (res.status) {
        resolve()
      }
    })
  })
})
